package com.lucene.blog.controller.admin;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.lucene.blog.bean.Blogger;
import com.lucene.blog.db.service.BloggerService;
import com.lucene.blog.util.CryptographyUtil;
import com.lucene.blog.util.DateUtil;
import com.lucene.blog.util.ResponseUtil;

import net.sf.json.JSONObject;

/**
 * 后台博主Controller
 * @author yachao
 *
 */
@Controller
@RequestMapping("/admin/blogger")
public class BloggerAdminController {

	@Resource
	private  BloggerService bloggerService;
	
	/**
	 * 查询博主信息
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findBlogger")
	public String findBlogger(HttpServletResponse response) throws Exception {
		Blogger blogger = bloggerService.getBloggerData();
		JSONObject result = JSONObject.fromObject(blogger);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 修改博主信息
	 * @param headPortrait
	 * @param blogger
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(@RequestParam("headPortraitFile") MultipartFile headPortraitFile,
					   Blogger blogger,
					   HttpServletRequest request,
					   HttpServletResponse response) throws Exception {
		if(!headPortraitFile.isEmpty()) { // 如果用户上传过头像就更新
			String filePath = request.getServletContext().getRealPath("/");
			String headPortraitName = DateUtil.getCurrentDateStr() + "." + headPortraitFile.getOriginalFilename().split("\\.")[1];
			headPortraitFile.transferTo(new File(filePath + "/headPortraits/" + headPortraitName));
			blogger.setHeadPortrait(headPortraitName);
		}
		int resultTotal = bloggerService.updateBlogger(blogger);
		JSONObject result = new JSONObject();
		
		if(resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 修改博主密码
	 * @param password
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyPassword")
	public String modifyPassword(@RequestParam("password") String password,
								 HttpServletResponse response) throws Exception {
		Blogger blogger = new Blogger();
		blogger.setPassword(CryptographyUtil.md5(password, "javacoder"));
		int resultTotal = bloggerService.updateBlogger(blogger);
		
		JSONObject result = new JSONObject();
		if(resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 退出系统
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/logout")
	public String logout() throws Exception {
		SecurityUtils.getSubject().logout();
		return "redirect:/login.jsp";
	}
}
